package com.coollang.smartbra.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.coollang.smartbra.base.MvpBaseView;
import com.coollang.smartbra.presenter2.AbstractMvpPresenter;

/**
 * @author Timmy
 *         指定子类具体的View必须继承自MvpBaseView
 *         指定子类具体的Presenter必须继承自AbstractMvpPersenter
 */
public abstract class AbstractMvpActivity<V extends MvpBaseView, P extends AbstractMvpPresenter<V>>
        extends AppCompatActivity implements MvpBaseView {

    private P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (mPresenter == null) {
            mPresenter = createPresenter();
        }

        if (mPresenter == null) {
            throw new NullPointerException("p 层不可以为空!");
        }

        // 绑定view
        mPresenter.attachMvpView((V) this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            // 解除绑定
            mPresenter.detachMvpView();
        }
    }

    /**
     * 创建 P 层
     *
     * @return
     */
    protected abstract P createPresenter();

    public P getPresenter() {
        return mPresenter;
    }
}
